<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/3/8
 * Time: 17:26
 */

namespace App\Rep;


use App\Models\Color;
use App\Models\ModelNumber;
use App\Models\ModelNumberColor;
use App\Models\ModelNumberSpecification;
use App\Models\ModelNumberUser;
use App\Models\Models;
use App\Models\Product;
use App\Models\ProductCenter;
use App\Models\ProductJobType;
use App\Models\ProductModelNumber;
use Illuminate\Support\Facades\DB;

class ModelNumberRep
{

    /**
     * 获取型号列表
     * @param $params
     * @return mixed
     */
    public function getModelNumberListRep($params){
        if(isset($params['product_id']) && !empty($params['product_id'])){
            $id_arr = ProductModelNumber::where(function($q)use($params){
                if(isset($params['model_number_id']) && !empty($params['model_number_id'])){
                    $q->where('model_number_id',$params['model_number_id']);
                }
                $q->where('product_id',$params['product_id']);
            })->pluck('model_number_id')->toArray();
            if(!empty($params['field_list'])){
                $select_arr = explode(',',$params['field_list']);
            }else{
                $select_arr = "*";
            }
            $model = ModelNumber::whereIn('id',$id_arr)->where('is_del',2);
            if(isset($params['is_dropdown']) && !empty($params['is_dropdown'])){
                if(isset($params['file'])){
                    if($params['file'] == 1){
                        array_push($select_arr,'file_id');
                        $model->with('file');
                    }
                }

                $list = $model->select($select_arr)->paginate($params['per_page']??1000);
            }else{
                if(isset($params['file'])){
                    if($params['file'] == 1){
                        array_push($select_arr,'file_id');
                        $model->with('file');
                    }
                }

                $list = $model->with(['product','model_number_model'=>function($q){
                    $q->with(['color_essential'=>function($q){
                        $q->select('id','color');
                    },'color_shelf'=>function($q){
                        $q->select('id','color');
                    },'color_match'=>function($q){
                        $q->select('id','color');
                    },'models'=>function($q){
                        $q->select('modelid','modelname');
                    },'product'=>function($q){
                        $q->select('id','product_name');
                    }]);
                }])->select($select_arr)->paginate($params['per_page']??1000);
            }
        }else{
            $list = ModelNumber::Search($params)->with(['product','model_number_model'=>function($q){
                $q->with(['color_essential'=>function($q){
                    $q->select('id','color');
                },'color_shelf'=>function($q){
                    $q->select('id','color');
                },'color_match'=>function($q){
                    $q->select('id','color');
                },'models'=>function($q){
                    $q->select('modelid','modelname');
                },'product'=>function($q){
                    $q->select('id','product_name');
                }]);
            }])->where('is_del',2)->with('file')->paginate($params['per_page']??1000);
        }

        return $list;
    }

    /**
     * 添加型号
     * @param $params
     * @return ModelNumber|array
     */
     public function addModelNumberRep($params){
         DB::beginTransaction();
         try {
             $obj = ModelNumber::updateOrCreate(
                 ['model_number'=>$params['model_number']],
                     ['model_number'=>$params['model_number'],'file_id'=>$params['file_id']]
             );
             ProductModelNumber::updateOrCreate(
                 ['product_id'=>$params['product_id'],'model_number_id'=>$obj->id],
                 ['product_id'=>$params['product_id'],'model_number_id'=>$obj->id]);

//             foreach($params['colors'] as $k=>$v){
//                 if(!empty($v['color_essential'])){
//                     $color_essential_obj = Color::updateOrCreate(['color'=>$v['color_essential']],['color'=>$v['color_essential']]);
//                 }
//                 if(!empty($v['color_shelf'])){
//                     $color_shelf_obj = Color::updateOrCreate(['color'=>$v['color_shelf']],['color'=>$v['color_shelf']]);
//                 }
//                 if(!empty($v['color_match'])){
//                     $color_match_obj = Color::updateOrCreate(['color'=>$v['color_match']],['color'=>$v['color_match']]);
//                 }
//                 $where = [
//                     'model_number_id'=>$obj->id,
//                     'semi_modelid'=>$v['modelid']
//                 ];
//                 ModelNumberColor::updateOrCreate($where,[$where,'color_essential'=>$color_essential_obj->id??'',
//                     'color_shelf'=>$color_shelf_obj->id??'',
//                     'color_match'=>$color_match_obj->id??'',
//                     'product_id'=>$v['product_id']??null]);
//             }
             DB::commit();
             return ['code'=>0,'data'=>[],'msg'=>''];
         }catch(\Exception $e){
             DB::rollBack();
             return ['code'=>2001,'data'=>[],'msg'=>$e->getMessage()];
         }
     }

     public function alertModelNumberRep($params){
         DB::beginTransaction();
         try {
             $model_number = ModelNumber::find($params['model_number_id']);
             $model_number->model_number = $params['model_number'];
             $model_number->file_id = $params['file_id'];
             $model_number->save();
             ProductModelNumber::where('model_number_id',$params['model_number_id'])->delete();
             ProductModelNumber::updateOrCreate(
                 ['product_id'=>$params['product_id'],'model_number_id'=>$params['model_number_id']],
                 ['product_id'=>$params['product_id'],'model_number_id'=>$params['model_number_id']]);
//             ModelNumberColor::where('model_number_id',$params['model_number_id'])->delete();
//             foreach($params['colors'] as $k=>$v){
//                 $color_essential_obj = [];
//                 if(!empty($v['color_essential'])){
//                     $color_essential_obj = Color::updateOrCreate(['color'=>$v['color_essential']],['color'=>$v['color_essential']]);
//                 }
//                 $color_shelf_obj = [];
//                 if(!empty($v['color_shelf'])){
//                     $color_shelf_obj = Color::updateOrCreate(['color'=>$v['color_shelf']],['color'=>$v['color_shelf']]);
//                 }
//                 $color_match_obj = [];
//                 if(!empty($v['color_match'])){
//                     $color_match_obj = Color::updateOrCreate(['color'=>$v['color_match']],['color'=>$v['color_match']]);
//                 }
//                 $where = [
//                     'model_number_id'=>$params['model_number_id'],
//                     'semi_modelid'=>$v['modelid'],
//                 ];
//                 ModelNumberColor::updateOrCreate($where,[$where,'color_essential'=>$color_essential_obj->id??'',
//                     'color_shelf'=>$color_shelf_obj->id??'',
//                     'color_match'=>$color_match_obj->id??'','product_id'=>$v['product_id']
//                 ]);
//             }
             DB::commit();
             return ['code'=>0,'data'=>[],'msg'=>''];
         }catch(\Exception $e){
             DB::rollBack();
             return ['code'=>2001,'data'=>[],'msg'=>$e->getMessage()];
         }

     }


    /**
     * 产品关联型号
     * @param $params
     * @return array
     */
    public function createModelNumberRep($params){
        DB::beginTransaction();
        try {
            if (isset($params['model_number_id']) && !empty($params['model_number_id'])) {
                $model = ModelNumber::where('id', $params['model_number_id'])->first();
            } else {
                $model = new ModelNumber();
            }
            //p($model->toArray());
            $model->model_number = $params['model_number'];
            if (isset($params['file_id']) && !empty($params['file_id'])) {
                $model->file_id = $params['file_id'];
            }
            $model->save();
            $product_id = $params['product_id'];
            ProductModelNumber::updateOrCreate(
                ['model_number_id'=>$model->id,'product_id'=>$product_id],
                ['model_number_id'=>$model->id,'product_id'=>$product_id]
            );
            DB::commit();
            return ['code'=>0,'data'=>$model,'msg'=>''];
        }catch(\Exception $e){
            DB::rollBack();
            return ['code'=>2001,'data'=>[],'msg'=>$e->getMessage()];
        }
    }

    /**
     * 删除型号
     * @param $params
     */
    public function delModelNumberRep($params){
        $model = ModelNumber::where('id',$params['id'])->first();
        $model->is_del = 1;
        $model->save();
//        $where = ['model_number_id'=>$params['id'],'product_id'=>$params['product_id']];
//        ProductModelNumber::where($where)->delete();
        ProductModelNumber::where('model_number_id',$params['id'])->delete();
        ModelNumberSpecification::where('model_number_id',$params['id'])->delete();
        return $model;
    }

    public function delProductModelnumberRep($params){
        $where = ['model_number_id'=>$params['model_number_id'],'product_id'=>$params['product_id']];
        ProductModelNumber::where($where)->delete();
        return true;
    }


    /**
     * 批量添加型号
     * @param $params
     */
    public function batchAddModelNumberRep($params){
        $array = $params['model_number_ids'];
        foreach($array as $k=>$v){
            $where = ['model_number_id'=>$v,'product_id'=>$params['product_id']];
            ProductModelNumber::updateOrCreate($where,$where);
        }
        return ['code'=>0,'data'=>[],'msg'=>''];
    }


    //获取型号产品列表
    public function getProductInfoByModelNumberIdRep($model_number_id,$modelid){
        $list = Models::where('pid',$modelid)->select('modelid','modelname','is_change')->get()->toArray();
        //p($list);
//        if(!empty($list)){
//            foreach($list as $k=>$v){
//                $list[$k]['default'] = ModelNumberColor::where('model_number_id',$model_number_id)
//                    ->where('semi_modelid',$v['modelid'])
//                    ->first();
//            }
//        }

        return $list;
    }

    //根据产品id获取型号列表
    public function getModelNumberListByProductRep($product_id){
        $product = Product::find($product_id);
        $id_arr = ProductModelNumber::where('product_id',$product_id)->pluck('model_number_id')->toArray();
        $list = ModelNumber::whereIn('id',$id_arr)->get();
        return $list;
    }


    //根据型号id获取主色id
    public function getColorByModelNumberIdRep($model_number_id){
        $info = ModelNumberColor::where('model_number_id',$model_number_id)->where('color_essential','<>',0)->value('color_essential');
        return $info;
    }

    //型号-工序关联员工
    public function setModelNumberUserRep($params){
        foreach($params['param_data'] as $k=>$v){
            if(!empty($v['users'])){
                ModelNumberUser::where('model_number_id',$v['model_number_id'])
                    ->where('job_type_id',$v['job_type_id'])->delete();
                foreach($v['users'] as $kk=>$vv){
                    $where = [
                        'model_number_id'=>$v['model_number_id'],
                        'job_type_id'=>$v['job_type_id'],
                        'user_id'=>$vv
                    ];

                    $obj = ModelNumberUser::updateOrCreate(
                        $where,$where
                    );
                }
            }else{
                $where = [
                    'model_number_id'=>$v['model_number_id'],
                    'job_type_id'=>$v['job_type_id'],
                    'user_id'=>''
                ];
                ModelNumberUser::where('model_number_id',$v['model_number_id'])
                    ->where('job_type_id',$v['job_type_id'])->delete();
                $obj = ModelNumberUser::updateOrCreate(
                    $where,$where
                );
            }
        }

        return $obj;
    }

    public function getModelNumberUserRep($params){
        $job_type_list = (new ProductRep())->getProductJobTypeListByProductId($params['product_id']);
        $list = [];
        foreach($job_type_list as $k=>$v){
            $list[$k]['job_type'] = $v;
            $arr = ModelNumberUser::where('model_number_id',$params['model_number_id'])
                ->where('job_type_id',$v['id'])
                ->pluck('user_id')->toArray();
            if(!empty($arr)){
                if($arr[0] == 0){
                    $arr = [];
                }
            }

            $list[$k]['user_id_arr'] = $arr;
        }
        return $list;
    }

    public function getUserListByModelNumberIdRep($params){
        return ModelNumberUser::where('model_number_id',$params['model_number_id'])
            ->where('job_type_id',$params['job_type_id'])
            ->with(['user'])
            ->get();
    }

    /**
     * 获取产品单价
     * @param $params
     * @return void
     */
    public function getProductUnitPriceRep($params){
        $model = ModelNumberSpecification::where('product_id',$params['product_id']);
        if(!empty($params['model_number_id'])){
            $model->where('model_number_id',$params['model_number_id']);
        }
        if(!empty($params['specification_id'])){
            $model->where('specification_id',$params['specification_id']);
        }
        $model->with(['product'=>function($q){
            $q->select('id','product_name');
        },'model_number','specification']);
        $data = $model->get();
        //p($model);
        //p($model->toArray());

        return $data;
    }

    /**
     * 修改产品单价
     * @param $params
     * @return void
     */
    public function updateProductUnitPriceRep($params){
        $where = [];
        if(!empty($params['product_id'])){
            $where['product_id'] = $params['product_id'];
        }
        if(!empty($params['model_numnber_id'])){
            $where['model_numnber_id'] = $params['product_id'];
        }
        if(!empty($params['specification_id'])){
            $where['specification_id'] = $params['specification_id'];
        }
        $obj = ModelNumberSpecification::updateOrCreate(
            $where,
            [
                'unit_price'=>$params['unit_price']
            ]
        );
        return $obj;
    }


    /**
     * 产品-型号-规格关联
     * @param $params
     * @return void
     */
    public function setModelNumberSpecificationRep($params){

        $job_type_list = ProductJobType::where('product_id',$params['product_id'])->get();
        $product_center_sid_arr = ProductCenter::where('product_id',$params['product_id'])
            ->where('model_number_id',$params['model_number_id'])
            ->pluck('specification_id')->toArray();
        $product_center_sid_arr = array_unique($product_center_sid_arr);


        ModelNumberSpecification::where('product_id',$params['product_id'])
            ->where('model_number_id',$params['model_number_id'])
            ->delete();
        foreach($product_center_sid_arr as $k=>$v){
            if(!in_array($v,$params['specification_ids'])){
                ProductCenter::where('product_id',$params['product_id'])
                    ->where('model_number_id',$params['model_number_id'])
                    ->where('specification_id',$v)->delete();
            }
        }
        foreach($params['specification_ids'] as $k=>$v){
            $insertArr[] = ['product_id'=>$params['product_id'],'model_number_id'=>$params['model_number_id'],'specification_id'=>$v];
            if($job_type_list->count()>0){
                foreach($job_type_list as $kk=>$vv){
                    ProductCenter::updateOrCreate(
                        [
                            'product_id'=>$params['product_id'],
                            'model_number_id'=>$params['model_number_id'],
                            'specification_id'=>$v,
                            'job_type_id'=>$vv->job_type_id
                        ],
                        [
                            'product_id'=>$params['product_id'],
                            'model_number_id'=>$params['model_number_id'],
                            'specification_id'=>$v,
                            'job_type_id'=>$vv->job_type_id
                        ]
                    );
                }
            }
        }
        ModelNumberSpecification::insert($insertArr);
        return true;

    }



}
